package ar.edu.unq.tpi.uis.tepe3.wicket.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import ar.edu.unq.tpi.uis.tepe3.dominio.Group;
import ar.edu.unq.tpi.uis.tepe3.dominio.SvnManager;
import ar.edu.unq.tpi.uis.tepe3.dominio.exception.PersistenceException;
import ar.edu.unq.tpi.uis.tepe3.wicket.AbstractListPage;
import ar.edu.unq.tpi.uis.tepe3.wicket.WicketApplication;
import ar.edu.unq.tpi.uis.tepe3.wicket.panels.DefaultCreateNewMenuPanel;
import ar.edu.unq.tpi.uis.tepe3.wicket.panels.DefaultListViewActionsPanel;

public class GroupListPage extends AbstractListPage {

	public GroupListPage() {
		super("Listado de Grupos");
		add(new ListView("listview", getListViewDataSource()) {
            protected void populateItem(ListItem item) {
                Group aGroup= (Group) item.getModelObject();
                item.add(new Label("name", aGroup.getId()));
                item.add(new DefaultListViewActionsPanel("actions",item.getModel()));
            }
        });
		
    }

	private List<Group> getListViewDataSource() {
		List<Group> dataSource = new ArrayList<Group>();
		
		SvnManager aSvnManager = ((WicketApplication)this.getApplication()).getModel();
		dataSource = aSvnManager.getGroups(); 
		return dataSource;
	}

	@Override
	public void deleteAction(IModel model) {
		
		try {
			((WicketApplication) this.getApplication()).getModel().removeGroup((Group) model.getObject());
			((WicketApplication) this.getApplication()).save();
			deleteSucceed();
		} catch (PersistenceException e) {
			warn(e.getMessage());
		}
	}
	
	@Override
	protected void addLeftMenu() {
		DefaultCreateNewMenuPanel aMenu = new DefaultCreateNewMenuPanel("leftMenu", new CompoundPropertyModel(new Group("")));
		add(aMenu);
	}

	
	
}
